package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-flow/cache"
	"logi-flow/model"
	"strconv"
	"time"
)

/*
SELECT
  per_orders.id OrderId,
  people.per_no PerNo,
  people.per_name PerName,
  per_orders.order_no OrderNo,
  per_orders.order_to OrderTo
FROM
  per_orders
  LEFT JOIN people ON people.id = per_orders.pid
WHERE per_orders.id=1
*/

func PersonOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var personOrderList model.PerOrderList
	redis_key := "person_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("per_orders").
			Select(
				"per_orders.id OrderId",
				"people.per_no PerNo",
				"people.per_name PerName",
				"per_orders.order_no OrderNo",
				"per_orders.order_to OrderTo",
			).Joins("LEFT JOIN people ON people.id = per_orders.pid").
			Where("per_orders.id=?", id).
			Find(&personOrderList)
		jsonData, _ := json.Marshal(personOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &personOrderList)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": personOrderList,
	})
}
func PersonOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var total int64
	model.DB.Model(&model.PerOrder{}).Count(&total)
	redis_key := "person_order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	/*
		SELECT
		  per_orders.id OrderId,
		  people.per_no PerNo,
		  people.per_name PerName,
		  per_orders.order_no OrderNo,
		  per_orders.order_to OrderTo
		FROM
		  per_orders
		  LEFT JOIN people ON people.id = per_orders.pid
	*/
	var personOrderList []model.PerOrderList
	if cacheData == "" {
		model.DB.Table("per_orders").
			Select(
				"per_orders.id OrderId",
				"people.per_no PerNo",
				"people.per_name PerName",
				"per_orders.order_no OrderNo",
				"per_orders.order_to OrderTo",
			).
			Joins("LEFT JOIN people ON people.id = per_orders.pid").
			Offset(offset).Limit(size).Find(&personOrderList)
		jsonData, _ := json.Marshal(personOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &personOrderList)
	}
	c.JSON(200, gin.H{
		"code":  200,
		"mag":   "ok",
		"data":  personOrderList,
		"total": total,
	})
}

func PersonOrderAdd(c *gin.Context) {
	pid, _ := strconv.Atoi(c.PostForm("pid"))
	order_no := c.PostForm("order_no")
	order_to := c.PostForm("order_to")
	data := model.PerOrder{
		Pid:     pid,
		OrderNo: order_no,
		OrderTo: order_to,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"mag":  "添加失败",
		})
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func PersonAdd(c *gin.Context) {
	per_no := c.PostForm("per_no")
	per_name := c.PostForm("per_name")
	if per_no == "" || per_name == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.Person{
		PerNo:   per_no,
		PerName: per_name,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"mag":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
